//
//  DetailHeaderView.swift
//  Jock
//
//  Created by HD on 15/3/19.
//  Copyright (c) 2015年 Haidy. All rights reserved.
//

import UIKit

class DetailHeaderView: UIView {

    @IBOutlet weak var iconView: UIImageView!
    @IBOutlet weak var nickView: UILabel!
    @IBOutlet weak var contentView: UILabel!
    @IBOutlet weak var contentImage: UIImageView!
    @IBOutlet weak var likeView: UILabel!
    @IBOutlet weak var unlikeView: UILabel!
    @IBOutlet weak var commentView: UILabel!
    
    var news: News?
    
    func showData() {
        if news != nil {
            if let user = news?.user {
                nickView.text = user.login
                if user.icon != nil {
                    var userIcon = user.icon
                    var userId =  user.id as NSString
                    var prefixUserId = userId.substringToIndex(4)
                    var url = "http://pic.qiushibaike.com/system/avtnew/\(prefixUserId)/\(userId)/thumb/\(userIcon)"
                    ImageLoader.loadImage(url, imageCacheDir: "iconCache", placeHolderName: "avatar.jpg", loadImageView: iconView)
                }
            } else {
                nickView.text = "匿名"
                iconView.image = UIImage(named: "avatar.jpg")
            }
            contentView.text = news?.content
            if let imageString = news?.image {
                var imageId = news!.id as NSString
                var prefiximageId = imageId.substringToIndex(5)
                var imagURL = "http://pic.qiushibaike.com/system/pictures/\(prefiximageId)/\(imageId)/medium/\(imageString)"
                self.contentImage.hidden = false
                ImageLoader.loadImage(imagURL, imageCacheDir: "imageCacheHD", placeHolderName: "avatar.jpg",  callBack: { (image) -> Void in
                    let height = image.size.height * (self.frame.width - 16) / image.size.width
                    let size = CGSize(width: self.contentImage.bounds.width, height: height)
                    self.contentImage.image = image.scaleToSize(size)
                    self.contentImage.setHeight(height)
                })
            } else {
                contentImage.hidden = true
            }
            likeView.text = "赞(\(news!.votesUp))"
            unlikeView.text = "踩(\(news!.votesDown))"
            commentView.text = "评论(\(news!.comments_count))"
        }
    }

}
